package com.lancoo.znbkxx.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.znbk.znbklibrary.util.LancooUtils;
import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.beans.SamplePaperBean;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Create by TangJun on 2019/2/16
 */
public class AllPaperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int VIEW_TYPE_NEW_J = 0;
    public static final int VIEW_TYPE_NEW_G = 1;
    public static final int VIEW_TYPE_NEW_M = 2;
    public static final int VIEW_TYPE_UNCOMPLETE = 3;
    public static final int VIEW_TYPE_COMPLETE = 4;
    private StringBuilder keyString;
    private List<SamplePaperBean> mData;

    private AllPaperAdapter.OnItemClickListener onItemClickListener;

    public AllPaperAdapter(List<SamplePaperBean> data) {
        this.mData = data;
        keyString = new StringBuilder();
    }
    public void updateData(List<SamplePaperBean> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    public void setKeyString(String key) {
        if (TextUtils.isEmpty(keyString.toString())) {
            keyString.append(key);
        } else {
            keyString.setLength(0);
            keyString.append(key);
        }
    }
    @SuppressLint("ResourceAsColor")
    private SpannableString matcherSearchText(String text) {
        String string = text.toLowerCase();
        String key = keyString.toString().toLowerCase();
        Pattern pattern = Pattern.compile(key);
        Matcher matcher = pattern.matcher(string);
        SpannableString ss = new SpannableString(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(Color.rgb(0,153,255)), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public void setOnItemClickListener(AllPaperAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        RecyclerView.ViewHolder myViewHolder = null;
        switch (i){
            case VIEW_TYPE_NEW_J:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_j_a, parent, false));
                break;
            case VIEW_TYPE_NEW_G:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_g_a, parent, false));
                break;
            case VIEW_TYPE_NEW_M:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_new_m_a, parent, false));
                break;
            case VIEW_TYPE_COMPLETE:
                myViewHolder = new CompleteVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_complete_a, parent, false));
                break;
            case VIEW_TYPE_UNCOMPLETE:
                myViewHolder = new UnCompleteVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_uncomplete_a, parent, false));
                break;
        }
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        SamplePaperBean samplePaperBean = mData.get(i);
        if (holder instanceof CompleteVH){
            CompleteVH completeVH = (CompleteVH)holder;
            completeVH.nameTv.setText(samplePaperBean.getPaperName());
            completeVH.mScore.setText(Math.round(samplePaperBean.getScore())+"");
            completeVH.mRange.setText(samplePaperBean.getRank()+"");
            completeVH.hScore.setText(samplePaperBean.getHighestScore()+"");
            completeVH.aScore.setText(samplePaperBean.getAverageScore()+"");
            completeVH.tvWatch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("查看","==================查看一下下=========000000=========");
                }
            });
        }else if (holder instanceof UnCompleteVH){
            UnCompleteVH unCompleteVH = (UnCompleteVH)holder;
            int subc = samplePaperBean.getAnswerCount();
            int totc = samplePaperBean.getTotalCount();
            float pec = subc / totc * 100;
            unCompleteVH.mPercentage.setText(Math.round(pec)+"");
            unCompleteVH.mBg.setProgress(Math.round(pec));
            unCompleteVH.nameTv.setText(samplePaperBean.getPaperName());
            unCompleteVH.subNum.setText(samplePaperBean.getSubmitStuCount()+"");
            unCompleteVH.totalNum.setText(samplePaperBean.getTotalStuCount()+"");
            unCompleteVH.hScore.setText(samplePaperBean.getHighestScore()+"");
            unCompleteVH.tvAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }else if (holder instanceof NotDownVH){
            NotDownVH notDownVHvh = (NotDownVH)holder;
            notDownVHvh.nameTv.setText(samplePaperBean.getPaperName());
            notDownVHvh.subNum.setText(samplePaperBean.getSubmitStuCount()+"");
            notDownVHvh.totalNum.setText(samplePaperBean.getTotalStuCount()+"");
            notDownVHvh.hScore.setText(samplePaperBean.getHighestScore()+"");
            notDownVHvh.tvAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        SamplePaperBean samplePaperBean = mData.get(position);
        if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 0){
            return VIEW_TYPE_NEW_J;
        }else if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 1){
            return VIEW_TYPE_NEW_G;
        }else if (samplePaperBean.getState() == 0 && samplePaperBean.getLayer() == 2){
            return VIEW_TYPE_NEW_M;
        }else if (samplePaperBean.getState() == 3){
            return VIEW_TYPE_COMPLETE;
        }else {
            return VIEW_TYPE_UNCOMPLETE;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    static class NotDownVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView subNum;
        TextView totalNum;
        TextView hScore;
        TextView tvAnswer;

        NotDownVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_page_name);
            subNum = itemView.findViewById(R.id.tv_submit_num);
            totalNum = itemView.findViewById(R.id.tv_total_num);
            hScore = itemView.findViewById(R.id.tv_score);
            tvAnswer = itemView.findViewById(R.id.tv_answer);
        }
    }
    static class CompleteVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView mScore;
        TextView aScore;
        TextView hScore;
        TextView mRange;
        TextView tvWatch;
        CompleteVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_page_name);
            mScore = itemView.findViewById(R.id.tv_my_score);
            aScore = itemView.findViewById(R.id.tv_average);
            hScore = itemView.findViewById(R.id.tv_height);
            mRange = itemView.findViewById(R.id.tv_range);
            tvWatch = itemView.findViewById(R.id.tv_watch);
        }
    }
    static class UnCompleteVH extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView subNum;
        TextView totalNum;
        TextView hScore;
        TextView mPercentage;
        ProgressBar mBg;
        TextView tvAnswer;
        UnCompleteVH(View itemView) {
            super(itemView);

            nameTv = itemView.findViewById(R.id.tv_page_name);
            subNum = itemView.findViewById(R.id.tv_submit_num);
            totalNum = itemView.findViewById(R.id.tv_total_num);
            hScore = itemView.findViewById(R.id.tv_score);
            mPercentage = itemView.findViewById(R.id.tv_percentage);
            mBg = itemView.findViewById(R.id.pb_percentage);
            tvAnswer = itemView.findViewById(R.id.tv_answer);
        }
    }
}
